<style type="text/css">
.addrecordtd{ width:100%px}
</style>
<h2>管理成绩</h2>
<script type="text/javascript">
function courseChanged()
{
	var selectedCourse=$("#courseName").val();
	$.ajax({
            type: 'Post',
            url: '/mngrecord_action_searchExamByCourse',
            data: "courseName="+selectedCourse,
			dataType:"json",
            success: function(response) {
                buildUpExamSelect(response);
            }
        })
}

function buildUpExamSelect(data)
{
	
	$("#examName option").remove();
	
	$("#examName").append("<option value='qingxuanze'>-请选择-</option>");
	$(data).each(function(i){
		$("#examName").append("<option value='Value'>"+this.examName+"</option>");
	})
}

function searchRecords()
{	
	
	var courseName=$('#courseName').val();
	var examName=$('#examName').val();
	if(courseName=='qingxuanze' || examName=='qingxuanze' )
	{
		alert('请先选择~');
	}
	else
	{
		var examNameText=$("#examName").find("option:selected").text();
		//alert(examNameText)
		//alert(courseName)
		ListAllRecordUnderCurrentClass(examNameText,courseName);
	}
}

function ListAllRecordUnderCurrentClass(examName,courseName)
{
	$.ajax({
            type: 'Post',
            url: '/mngrecord_action_searchRecordByExamAndCourse',
            data: "examName="+examName+"&courseName="+courseName,
			dataType:"json",
            success: function(response) {
                BuildRecordTable(response);
            }
        })
}
function BuildRecordTable(data)
{
	$("#mngexam_table").find("tr[id!='header']&&[id!='template']").remove();
	var table=$("#mngexam_table")
	$(data).each(function(i){
			var row = $("#mngexam_table").find('#template').clone();
			$(row).find("#stuName").find("div").html(this.studentName);
			$(row).find("#stuNumber").find("div").html(this.studentNumber);
			$(row).find("#recordValue").find("div").html(this.recordValue);
			$(row).attr("id","ready");
			$(row).removeClass("hiddenTd");
			if(i%2==0)
			{
				$(row).addClass("colorTr");
				}
            row.appendTo(table);
			})
}
function recordDelete_clicked(sender)
{
	alert("try delete")
}
function recordUpdate_clicked(sender)
{
	var updateInput=$(sender).parent().parent().find("#updateInput")
	var showData=$(sender).parent().parent().find("#showData")
	var updateLink=$(sender).parent().parent().find("#updateLink")
	var saveLink=$(sender).parent().parent().find("#saveLink")
	$(updateInput).removeClass("hide");
	$(showData).addClass("hide");
	$(updateLink).addClass("hide");
	$(saveLink).removeClass("hide");
	$(updateInput).val($(showData).html());
}
function recordSave_clicked(sender)
{
	
	var updateInput=$(sender).parent().parent().find("#updateInput")
	var showData=$(sender).parent().parent().find("#showData")
	var updateLink=$(sender).parent().parent().find("#updateLink")
	var saveLink=$(sender).parent().parent().find("#saveLink")
	if(!CheckNumber(updateInput.val()))
	{
		alert("输入数字")
	}
	else if($(updateInput).val()==showData.html())
	{
		return;
	}
	else{
		var courseName=$('#courseName').val();
		var examName=$('#examName').val();
		var examNameText=$("#examName").find("option:selected").text();
		var studentNumber=$(sender).parent().parent().find("#stuNumber div").html();
		var newValue=$(updateInput).val();
		 
		$.ajax({
            type: 'Post',
            url: '/mngrecord_action_updateSingleRecord',
            data: "examName="+examNameText+"&courseName="+courseName+"&studentNumber="+studentNumber+"&newValue="+newValue,
            success: function(response) {
                if(response=='saved')
				{
					showData.html($(updateInput).val());
					return;
				}
				else
				{
					alert('更新失败')
				}
            }
        })
	}
	$(updateInput).addClass("hide");
	$(showData).removeClass("hide");
	$(updateLink).removeClass("hide");
	$(saveLink).addClass("hide");
}


</script>
<table>
  <tr>
    <td>课程名称：</td>
    <td><select id="courseName" onchange="javascript:courseChanged()">
        <option value="qingxuanze">-请选择-</option>
        <option value="yuwen">语文</option>
        <option value="shuxue">数学</option>
        <option value="yingyu">英语</option>
        <option value="wuli">物理</option>
        <option value="huaxue">化学</option>
        <option value="lishi">历史</option>
        <option value="dili">地理</option>
        <option value="shengwu">生物</option>
        <option value="zhengzhi">政治</option>
        <option value="qita">其他</option>
      </select></td>
  <tr></tr>
  <td>考试名称：</td>
    <td><select id="examName">
        <option value="qingxuanze">-请选择-</option>
      </select></td>
  </tr>
  <tr>
  	<td>
		<input type="button" title="查找成绩"  value="查找成绩" onclick="javascript:searchRecords()"/>
	</td>
  </tr>
</table>
<table id="mngexam_table">
    <thead>
      <tr id="header">
        <th style="width:100px">姓名</th>
        <th style="width:100px">学号</th>
		<th style="width:100px">成绩</th>
        <th style="width:100px">操作记录</th>
      </tr>
    </thead>
    <tbody>
      <tr id="template" class="hiddenTd">
        <td style="text-align:center;" id="stuName"><div></div></td>
        <td style="text-align:center;" id="stuNumber"><div></div></td>
		<td style="text-align:center;" id="recordValue"><input type="text" style="width:60px" id="updateInput" class="hide"/><div id="showData"></div></td>
        <td style="text-align:center;" id="operateTd">
		<!--<a style="cursor:pointer; color:#CC9999" onclick="javascript:recordDelete_clicked(this)">删除</a>-->
		<a id="updateLink" style="cursor:pointer; color:#CC9999" onclick="javascript:recordUpdate_clicked(this)">修改</a>
		<a id="saveLink" style="cursor:pointer; color:#CC9999" onclick="javascript:recordSave_clicked(this)" class="hide">保存</a>
		</td>
		<td style=" display:none" id="courseTd_hidden"></td>
      </tr>
    </tbody>
  </table>
